package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Registlevel;
import com.foreknow.service.RegistlevelService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reg")
public class RegistlevelController {
    @Autowired
    private RegistlevelService registlevelService;

    /**
     * 分页查询所有挂号等级
     * @param param
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/findLevelByPage",method = RequestMethod.GET)
    @ResponseBody
    public String findLevelByPage(@RequestParam Map<String, Object> param) throws JsonProcessingException {
        int page=Integer.parseInt((String)param.get("pagenum"));
        int size=Integer.parseInt((String)param.get("pagesize")) ;
        PageHelper.startPage(page, size);
        List<Registlevel> levelList = registlevelService.getQueryAllLevel((String)param.get("query"));
        PageInfo<Registlevel> pageInfo = new PageInfo(levelList);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", pageInfo.getList());
        hs.put("total",pageInfo.getTotal());
        hs.put("meta", new Constance("msg", 200));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    /**
     * 添加
     * @param
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/addLevel",method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody Registlevel registlevel) throws JsonProcessingException {
        int x = registlevelService.insertLevel(registlevel);
        HashMap<String,Object> hs = new HashMap<>();
        hs.put("data",x);
        hs.put("meta",new Constance("msg",200));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    /**
     * 修改之前查询挂号等级
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/selectLevelById", method = RequestMethod.GET)
    @ResponseBody
    public String selectLevelById(@RequestParam Integer id) throws JsonProcessingException {
        Registlevel registlevel = registlevelService.selectLevel(id);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", registlevel);
        hs.put("meta", new Constance("msg", 200));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    /**
     * 修改挂号等级
     * @param
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/updateLevel",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody Registlevel registlevel) throws JsonProcessingException {
        int x= registlevelService.updateLevel(registlevel);
        HashMap<String,Object> hs = new HashMap<>();
        hs.put("data",x);
        hs.put("meta",new Constance("msg",200));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    /**
     * 删除
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/deleteLevel", method = RequestMethod.GET)
    @ResponseBody
    public String deleteLevelById(@RequestParam Integer id) throws JsonProcessingException {
        int x = registlevelService.deleteLevel(id);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("meta", new Constance("msg", 200));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    /**
     * 批量删除
     * @param param
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/deleteMany", method = RequestMethod.POST)
    @ResponseBody
    public String deleteList(@RequestBody() Object[] param) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        for (int i = 0; i < param.length; i++) {
            registlevelService.deleteLevel((Integer) param[i]);
        }
        hs.put("meta", new Constance("msg", 200));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}